#include "form_name_dlg.h"
#include "ui_form_name_dlg.h"

#include "utils/keyboard/keyboard_mgr.h"
#include "ui/share/form_prompt_dlg.h"

static int s_nReplyType = 0;

/*
 * Function:    FormNameDlg
 * Description: The default constructor
 * Paramer:     void
 * Return:
 */
FormNameDlg::FormNameDlg(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormNameDlg)
{
    qDebug() << Q_FUNC_INFO;

    ui->setupUi(this);
}

/*
 * FormNameDlg:    ~FormNameDlg
 * Description: The default destructor
 * Paramer:     void
 * Return:
 */
FormNameDlg::~FormNameDlg()
{
    qDebug() << Q_FUNC_INFO;

    delete ui;
}

/*
 * Function:    getInstance
 * Description: Get an instance of "FormNameDlg" class, if exist, return it
 * Paramer:     QWidget *pParent - the pointer of QWidget class
 * Return:      the pointer of "FormNameDlg" class
 */
FormNameDlg* FormNameDlg::getInstance(QWidget *pParent)
{
    static FormNameDlg *pNew = NULL;
    if(!pNew) {
        qDebug() << Q_FUNC_INFO;

        // Create a new object if it doesn't exist
        pNew = new FormNameDlg(pParent);
        if(NULL == pNew) return NULL;

        // After constructing object, initialize the object
        pNew->_initObj();

        // Initialize control's UI
        pNew->_InitUi();

        // Hide widget firstly
        pNew->hide();
    }
    else {
        // Set the parent object
        pNew->setParent(pParent);
    }

    return pNew;
}

/*
 * Function:    _initObj
 * Description: After constructing object, initialize the object
 * Paramer:     void
 * Return:      void
 */
void FormNameDlg::_initObj()
{
    qDebug() << Q_FUNC_INFO;
}

/*
* Function:		_InitUi
* Description:	Init the window style
* Paramer:		void
* Return:		void
*/
void FormNameDlg::_InitUi()
{
    qDebug() << Q_FUNC_INFO;

    // Displays the background image of the window
    QWidget::setAttribute(Qt::WA_StyledBackground);

    SET_STYLE_SHEET(QString(":/%1/factory/%2.qss").arg("skin_1").arg(this->objectName()));

    ui->lineEdit_Name->setValidator(new QRegExpValidator(QRegExp("^[\u4E00-\u9FA5A-Za-z0-9_-]{1,30}$")));
    ui->lineEdit_Consume->setValidator(new QRegExpValidator(QRegExp("^[0-9]{1,5}$")));
    ui->lineEdit_Consume->setInputMethodHints(Qt::ImhDigitsOnly);

    ui->checkBox_Export->setFocusPolicy(Qt::NoFocus);
    ui->pushButton_Confirm->setFocusPolicy(Qt::NoFocus);
    ui->pushButton_Cancel->setFocusPolicy(Qt::NoFocus);
}

/*
* Function:		changeEvent
* Description:	Triggers an event before it is switch language
* Paramer:		QShowEvent *event - the pointer of "QEvent"
* Return:		void
*/
void FormNameDlg::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);

    switch (e->type())
    {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);

        qDebug() << Q_FUNC_INFO;
        break;
    default:
        break;
    }
}

/*
* Function:		on_pushButton_Save_clicked
* Description:	Save the basic info after click the button
* Paramer:		void
* Return:		void
*/
void FormNameDlg::on_pushButton_Confirm_clicked()
{
    qDebug() << Q_FUNC_INFO;

    if(ui->lineEdit_Name->text().trimmed().isEmpty()) {
        FormPromptWidget::getInstance(this->parentWidget())->setPromptInfo(tr("Prompt"), tr("Please enter a name"));
        return;
    }

    // Signal-slot initialization
    bool bCon = QObject::connect(this, SIGNAL(signalReplayInfo(int, bool, QString)), this->parent(), SLOT(slotRespondInfoFromDlgEx(int, bool, QString)));
    Q_ASSERT(bCon);

    QTimer::singleShot(10, this, [=]{
        this->hide();
    });

    QString sContent = ui->lineEdit_Name->text().trimmed() + " " + QString::number(ui->lineEdit_Consume->text().trimmed().toUInt()) + " " + QString::number(ui->checkBox_Export->isChecked());

    emit signalReplayInfo(s_nReplyType, true, sContent);

    // Signal-slot disconnect
    QObject::disconnect(this, SIGNAL(signalReplayInfo(int, bool, QString)), this->parent(), SLOT(slotRespondInfoFromDlgEx(int, bool, QString)));
}

/*
* Function:		on_pushButton_Cancel_clicked
* Description:	Close the window after click the button
* Paramer:		void
* Return:		void
*/
void FormNameDlg::on_pushButton_Cancel_clicked()
{
    qDebug() << Q_FUNC_INFO;

    ui->lineEdit_Name->setText("");

    this->hide();
}

/*
 * Function:    displayWindow
 * Description: Set procedure name and display the page
 * Paramer:     int nReplyType - the operate type of procedure
 *              QString sFileName - the data file name
 *              bool bNewProcedure - Is a new procedure ?
 * Return:      void
 */
void FormNameDlg::displayWindow(int nReplyType, QString sContent, QString sContentEx)
{
    qDebug() << Q_FUNC_INFO << nReplyType << sContent << sContentEx;
    s_nReplyType = nReplyType;
    sContent = sContent.trimmed();

    ui->pushButton_Confirm->setEnabled(true);
    ui->label_Consume->setVisible(false);
    ui->lineEdit_Consume->setVisible(false);
    ui->checkBox_Export->setVisible(false);

    ui->lineEdit_Name->setText("");
    ui->lineEdit_Consume->setText("");

    switch (nReplyType) {
    case E_WA_FAC_EXTRACT_TEST_NEW:
    case E_WA_FAC_PROCESS_TEST_NEW:
    case E_WA_FAC_PCR_TEST_NEW:
    {
        ui->label_PageTitle->setText(tr("Save"));
        ui->label_Consume->setVisible(true);
        ui->lineEdit_Consume->setVisible(true);

        QString sProcName = QDateTime::currentDateTime().toString("MMdd_hh.mm.ss");
        QString sTimeConsume = (E_WA_FAC_EXTRACT_TEST_NEW == nReplyType) ? sContentEx : "0";
        ui->lineEdit_Name->setText(sProcName);
        ui->lineEdit_Consume->setText(sTimeConsume);

        break;
    }
    case E_WA_FAC_EXTRACT_TEST_EDIT:
    case E_WA_FAC_PROCESS_TEST_EDIT:
    case E_WA_FAC_PCR_TEST_EDIT:
    {
        ui->label_PageTitle->setText(tr("Save"));
        ui->label_Consume->setVisible(true);
        ui->lineEdit_Consume->setVisible(true);

        QStringList lstContent = sContent.split(" ");
        if(2 == lstContent.size()) {
            QString sProcName = lstContent.at(0).trimmed();
            QString sTimeConsume = lstContent.at(1).trimmed();

            ui->lineEdit_Name->setText(sProcName);
            ui->lineEdit_Consume->setText(sTimeConsume);
        }
        else {
            ui->pushButton_Confirm->setEnabled(false);
        }

        break;
    }
    case E_WA_FAC_EXTRACT_TEST_SAVE_AS:
    case E_WA_FAC_PROCESS_TEST_SAVE_AS:
    case E_WA_FAC_PCR_TEST_SAVE_AS:
    {
        ui->label_PageTitle->setText(tr("Save As"));
        ui->lineEdit_Name->setText(sContent);
        ui->checkBox_Export->setVisible(true);

        break;
    }
    case E_WA_FAC_SAMPLE_TYPE_NEW:
    {
        ui->label_PageTitle->setText(tr("New"));
        ui->lineEdit_Name->setText("");
        break;
    }
    case E_WA_FAC_SAMPLE_TYPE_EDIT:
    {
        ui->label_PageTitle->setText(tr("Edit"));
        ui->lineEdit_Name->setText(sContent);

        break;
    }
    default: break;
    }

    KeyboardMgr::getInstance()->setParent(this);
    this->show();
}
